package com.huanhong.hik.util.callback;

import com.alibaba.fastjson.JSONObject;
import com.huanhong.hik.bean.Result;
import com.huanhong.hik.socket.SocketIoClient;
import com.huanhong.hik.util.HCNetSDK;
import com.sun.jna.Pointer;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@NoArgsConstructor
public class FRemoteCfgCallBackFaceGet implements HCNetSDK.FRemoteConfigCallback {


    @Override
    public void invoke(int dwType, Pointer lpBuffer, int dwBufLen, Pointer pUserData) {
        log.info("长连接回调获取数据,NET_SDK_CALLBACK_TYPE_STATUS:{}", dwType);
        switch (dwType) {
            case 0:// NET_SDK_CALLBACK_TYPE_STATUS
                HCNetSDK.REMOTECONFIGSTATUS_CARD struCfgStatus = new HCNetSDK.REMOTECONFIGSTATUS_CARD();
                struCfgStatus.write();
                Pointer pCfgStatus = struCfgStatus.getPointer();
                pCfgStatus.write(0, lpBuffer.getByteArray(0, struCfgStatus.size()), 0, struCfgStatus.size());
                struCfgStatus.read();

                int iStatus = 0;
                for (int i = 0; i < 4; i++) {
                    int ioffset = i * 8;
                    int iByte = struCfgStatus.byStatus[i] & 0xff;
                    iStatus = iStatus + (iByte << ioffset);
                }

                switch (iStatus) {
                    case 1000:// NET_SDK_CALLBACK_STATUS_SUCCESS
                        log.info("查询人脸参数成功,dwStatus:{}", iStatus);
                        break;
                    case 1001:
                        log.info("正在查询人脸参数中,dwStatus:{}", iStatus);
                        break;
                    case 1002:
                        int iErrorCode = 0;
                        for (int i = 0; i < 4; i++) {
                            int ioffset = i * 8;
                            int iByte = struCfgStatus.byErrorCode[i] & 0xff;
                            iErrorCode = iErrorCode + (iByte << ioffset);
                        }
                        log.info("查询人脸参数失败, dwStatus:{},错误号:{}", iStatus, iErrorCode);
                        break;
                }
                break;
            case 2: //NET_SDK_CALLBACK_TYPE_DATA
                HCNetSDK.NET_DVR_FACE_PARAM_CFG mStruFaceInfo = new HCNetSDK.NET_DVR_FACE_PARAM_CFG();
                mStruFaceInfo.write();
                Pointer pInfoV30 = mStruFaceInfo.getPointer();
                pInfoV30.write(0, lpBuffer.getByteArray(0, mStruFaceInfo.size()), 0, mStruFaceInfo.size());
                mStruFaceInfo.read();
                String str = new String(mStruFaceInfo.byCardNo).trim();
                log.info("查询到人脸数据关联的卡号,getCardNo:{},人脸数据类型:{}", str, mStruFaceInfo.byFaceDataType);
                if (mStruFaceInfo.dwFaceLen > 0) {
                    SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
                    String newName = sf.format(new Date());
                    FileOutputStream fout;
                    try {
                        fout = new FileOutputStream(newName + "_Card[" + str + "]_ACSFaceCfg.jpg");
                        //将字节写入文件
                        long offset = 0;
                        ByteBuffer buffers = mStruFaceInfo.pFaceBuffer.getByteBuffer(offset, mStruFaceInfo.dwFaceLen);
                        byte[] bytes = new byte[mStruFaceInfo.dwFaceLen];
                        buffers.rewind();
                        buffers.get(bytes);
                        if (null != SocketIoClient.socket) {
                            JSONObject data = new JSONObject();
                            data.put("cardNo", str);
//                            data.put("img", "data:image/jpeg;base64," + Base64.encode(bytes));
                            data.put("imgBytes", bytes);
                            SocketIoClient.socket.emit("getFace", Result.success(data).toJsonString());
                        }
                        fout.write(bytes);
                        fout.close();
                    } catch (FileNotFoundException e) {
                        log.error("FileNotFoundException", e);
                    } catch (IOException e) {
                        log.error("IOException", e);
                    }
                }
                break;
            default:
                break;
        }
    }
}
